package com.lzc.tlmail.tlmailseckill.controller;

import com.lzc.common.utils.R;
import com.lzc.tlmail.tlmailseckill.service.SeckillService;
import com.lzc.tlmail.tlmailseckill.to.SeckillSkusInfoto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lzc
 * @create 2020-10-25 12:18
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 获取秒杀商品
     * @return
     */
    @ResponseBody
    @GetMapping("/seckillSessionSkus")
    public R getSeckillSessionSkus(){
       List<SeckillSkusInfoto> seckillSkusInfotos=seckillService.getSeckillSessionSkus();
       return R.ok().setData(seckillSkusInfotos);
    }

    /**
     * 获取对应sku信息
     * @return
     */
    @ResponseBody
    @GetMapping("/seckillSkuInfo/{skuId}")
    public R getSeckillSkuInfo(@PathVariable("skuId")Long skuId){
       SeckillSkusInfoto seckillSkusInfoto=seckillService.getSeckillSkuInfo(skuId);
        return R.ok().setData(seckillSkusInfoto);
    }


    @GetMapping("/kill")
    public String kill(@RequestParam("killId")String killId, @RequestParam("num")Integer num, @RequestParam("code")String code, Model model){
        String ordersn=seckillService.skuKill(killId,num,code);
        if(StringUtils.isEmpty(ordersn)){
            return "error";
        }else{
            model.addAttribute("ordersn",ordersn);
            return "success";
        }

    }
}
